﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ScrumTracker.Models;
using ScrumTracker.Helpers;

namespace ScrumTracker.Controllers
{
    public class ProductsController : Controller
    {
        //
        // GET: /Products/

        public ActionResult List()
        {
            return View("List");
        }

        public ActionResult Get(int id)
        {
            //var context = new ScrumTrackerEntities();
            //var product = context.Products.Single(p => p.ProductId == id);
            //ViewData.Model = product;
            //SessionHelper.CurrentProductId = product.ProductId;
            //return View("Backlog");
            SessionHelper.CurrentProductId = id;
            return RedirectToAction("backlog");
        }

        public ActionResult New()
        {
            ViewData.Model = new Product();
            return View();
        }

        public ActionResult Create()
        {
            var entity = new Product() { CreatedAt = DateTime.UtcNow };
            if (TryUpdateModel<Product>(entity))
            {
                var context = ScrumTrackerEntities.Instance;
                context.Products.AddObject(entity);
                context.SaveChanges();
                TempData["notice"] = "Sprint successfully added!";
                return RedirectToAction("Get", "Products", new { id = entity.ProductId });
            }
            return View("New");
        }

        public ActionResult Edit(int id)
        {
            //if (id == null)
            //{
            //    return List();
            //}
            //var context = new ScrumTrackerEntities();
            //ViewBag.Products = context.Products.Select(e => e).ToList();
            //ViewData.Model = context.Products.Single(p => p.ProductId == id);
            //Session["product_id"] = id;
            var context = new ScrumTrackerEntities();
            ViewData.Model = context.Products.Single(p => p.ProductId == id);
            ViewBag.ProductId = id;
            return View();
        }

        [HttpPost]
        public ActionResult Update(int id)
        {
            var context = ScrumTrackerEntities.Instance;
            var entity = context.Products.SingleOrDefault(e => e.ProductId == id);
            if (TryUpdateModel<Product>(entity))
            {
                context.ObjectStateManager.ChangeObjectState(entity, System.Data.EntityState.Modified);
                context.SaveChanges();
                TempData["notice"] = "Product successfully updated!";
                //if (Request.AcceptTypes.Contains("application/json"))
                //{
                //    return Json(new { success = true });
                //}
                //else
                //{
                //return RedirectToAction("Edit", "Products", new { id = entity.ProductId });
                //}
            }
            ViewData.Model = entity;
            return View("Edit");
        }

        public ActionResult Backlog(int id)
        {
            //var context = ScrumTrackerEntities.Instance;
            //var product = context.Products.Include("Stories").SingleOrDefault(p => p.ProductId == id);
            //var stories = product.Stories.Where(s => s.SprintId == null).OrderByDescending(s => s.Priority).ThenBy(s => s.Effort).ToList();
            //return PartialView("~/Views/Stories/_BacklogList.cshtml", stories);
            var context = new ScrumTrackerEntities();
            ViewData.Model = context.Products.Include("Stories.Tasks").Single(p => p.ProductId == id);
            return View();

        }

        [ValidateAntiForgeryToken]
        public ActionResult Delete(int id)
        {
            var context = ScrumTrackerEntities.Instance;
            var entity = context.Products.SingleOrDefault(e => e.ProductId == id);
            entity.IsDeleted = true;
            context.ObjectStateManager.ChangeObjectState(entity, System.Data.EntityState.Modified);
            context.SaveChanges();
            TempData["notice"] = "Product successfully deleted!";
            return Redirect("~");
        }
    }
}
